/***************************************************************************
      File Name:  Log.cs
   File Created:  Saturday, February 23rd 2008
      Copyright:  (C) 2008 by Darko Matesic
          Email:  darko.matesic@yahoo.com
 ***************************************************************************/

using System;
using System.IO;
using CallerID.Helpers;

namespace CallerID
{
    public class Log
    {
        private static string m_FileName = "";

        /// <summary>
        /// Gets or sets log File Name.
        /// </summary>
        public static string FileName
        {
            get { return m_FileName; }
            set { m_FileName = FileHelpers.AppendExtension(value, "txt"); }
        }

        /// <summary>
        /// Appends text to log with optional timestamp and optionally and advances to new line
        /// </summary>
        private static void AppendText(string text, bool timestamp, bool line)
        {
            if(m_FileName != null && m_FileName.Length > 0) {
                try {
                    StreamWriter stream = (File.Exists(m_FileName) ? File.AppendText(m_FileName) : File.CreateText(m_FileName));
                    if(stream != null) {
                        if(timestamp) {
                            string ts = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            if(line)
                                stream.WriteLine(ts + " : " + text);
                            else
                                stream.Write(ts + " : " + text);
                        } else {
                            if(line)
                                stream.WriteLine(text);
                            else
                                stream.Write(text);
                        }
                        stream.Flush();
                        stream.Close();
                        stream.Dispose();
                    }
                } catch { }
            }
        }

        /// <summary>
        /// Appends text to log with optional timestamp.
        /// </summary>
        public static void Append(string text, bool timestamp)
        {
            AppendText(text, timestamp, false);
        }
        /// <summary>
        /// Appends text to log.
        /// </summary>
        public static void Append(string text)
        {
            AppendText(text, false, false);
        }

        /// <summary>
        /// Appends text to log with optional timestamp and advances to new line.
        /// </summary>
        public static void AppendLine(string text, bool timestamp)
        {
            AppendText(text, timestamp, true);
        }
        /// <summary>
        /// Appends text to log and advances to new line.
        /// </summary>
        public static void AppendLine(string text)
        {
            AppendText(text, false, true);
        }

        /// <summary>
        /// Clears log.
        /// </summary>
        public static void Clear()
        {
            if(m_FileName != null && m_FileName.Length > 0) {
                try {
                    File.Delete(m_FileName);
                    StreamWriter stream = File.CreateText(m_FileName);
                    if(stream != null) {
                        stream.Close();
                        stream.Dispose();
                    }
                } catch { }
            }
        }

    }
}
